﻿Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmEffectiveDateNotice

    Sub FillStaff()
        Try
            Me.Cursor = Cursors.Default

            Dim cmd As New SqlCommand("select EmpNo,FullNameEn from StaffProfiles", cnn)
            Dim Reader As SqlDataReader

            Me.ListUsers.Items.Clear()
            Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListViewItem
                Item.Text = Reader.Item("EmpNo")
                Item.SubItems.Add(Reader.Item("FullNameEn"))
                Me.ListUsers.Items.Add(Item)
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmClearanceCertificate_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillStaff()
    End Sub

    Sub Clear()
        Me.txtName.Clear()
        Me.txtJobTitle.Clear()
        Me.txtBranch.Clear()
        Me.DTPStartWork.Value = Now
        Me.CrystalReportViewer1.ReportSource = Nothing
    End Sub

    Private Sub ListUsers_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListUsers.SelectedIndexChanged
        Clear()

        If Me.ListUsers.SelectedItems.Count > 0 Then
            Try
                Me.Cursor = Cursors.WaitCursor
                Dim cmd As New SqlCommand("Select EmpNo,FullNameEn,Position,Branch,Nationality " & _
                                          "From StaffProfiles where EmpNo=" & CInt(Me.ListUsers.SelectedItems(0).Text), cnn)
                Dim Reader As SqlDataReader

                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Me.txtEmpNo.Text = Reader.Item("EmpNo")
                    Me.txtName.Text = Reader.Item("FullNameEn")
                    Me.txtJobTitle.Text = Reader.Item("Position")
                    Me.txtBranch.Text = Reader.Item("Branch")
                End While
                Reader.Close()
                cnn.Close()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub btnPrintVacationRequestForm_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintVacationRequestForm.Click
        Me.ErrProv.Clear()
        If Me.txtName.Text.Trim.Length = 0 Then
            MsgBox("Please select a staff member from the list")
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim dap As New SqlDataAdapter("Select * From StaffProfiles Where EmpNo=" & Me.txtEmpNo.Text, cnn)
                Dim das As New DSstaffProfiles

                dap.Fill(das, "StaffProfiles")

                Dim rpt As New EffectiveDateNotice
                rpt.SetDataSource(das)

                Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
                Dim crParameterFieldDefinitions As ParameterFieldDefinitions
                Dim crParameterFieldLocation As ParameterFieldDefinition
                Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues
                crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields

                'VacationDaysTaken PARAMETER
                crParameterFieldLocation = crParameterFieldDefinitions.Item("Date")
                crParameterValues = crParameterFieldLocation.CurrentValues
                crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = Me.DTPStartWork.Value
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

                Me.CrystalReportViewer1.ReportSource = rpt

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub
End Class